﻿using Admin.EcsWeb.Service.PlcTag.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;

namespace Admin.EcsWeb.Service;
/// <summary>
/// PlcTagService服务
/// </summary>
[ApiDescriptionSettings("PLCTAG", Order = 120)]
public class PlcTagService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Entity.PlcTag> _rep;
    public PlcTagService(SqlSugarRepository<Entity.PlcTag> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询PlcTagService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<PlcTagOutput>> Page([FromQuery] PlcTagInput input)
    {
        var query = _rep.AsQueryable()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.TAGPROPERTY), u => u.TAGPROPERTY.Contains(input.TAGPROPERTY.Trim()))
                     .WhereIF(input.TAGNUMBER > 0, u => u.TAGNUMBER == input.TAGNUMBER)

                     .WhereIF(input.PLCGROUPNUMBER > 0, u => u.PLCGROUPNUMBER == input.PLCGROUPNUMBER)
                     .Where(x => x.IsDelete == false)

                    .Select<PlcTagOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加PlcTagService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddPlcTagInput input)
    {
        var entity = input.Adapt<Entity.PlcTag>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除PlcTagService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeletePlcTagInput input)
    {
        long.TryParse(input.Id, out long did);
        var entity = await _rep.GetFirstAsync(u => u.Id == did) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新PlcTagService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdatePlcTagInput input)
    {
        var entity = input.Adapt<Entity.PlcTag>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取PlcTagService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<Entity.PlcTag> Get([FromQuery] QueryByIdPlcTagInput input)
    {
        long.TryParse(input.Id, out long did);
        return await _rep.GetFirstAsync(u => u.Id == did);
    }

    /// <summary>
    /// 获取PlcTagService列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<PlcTagOutput>> List([FromQuery] PlcTagInput input)
    {
        return await _rep.AsQueryable().Select<PlcTagOutput>().ToListAsync();
    }





}

